﻿namespace CoCoonCoreWM6
{
    public class VDG
    {
        public Pixel[] GetFrame()
        {
            //check video mode
            //(assume Pmode 4 for now)
            
            //get memory offset
            //TODO assume 0x400 for now
            const int offset = 0x0400;

            //get page size
            //TODO assume 6144 for now
            const int pagesize = 6144;

            Pixel[] pixels = new Pixel[256 * 192];

            //iterate memory addresses and generate video signal 
            int pos = 0;
            for (int i = offset; i < pagesize; i++)
            {
                pos = AddPmode4Pixels(pixels, pos, RAM.Memory[i]);
            }
            return pixels;
        }

        private static int AddPmode4Pixels(Pixel[] pixels, int pos, byte b)
        {
            int t;
            for (t = 128; t > 0; t = t / 2)
            {
                if ((b & t) != 0)
                {
                    pixels[pos] = new Pixel(255, 255, 255);
                    pos++;
                }
                if ((b & t) == 0)
                {
                    pixels[pos] = new Pixel(0, 0, 0);
                    pos++;
                }
            }
            return pos;
        }
    }
}
